#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    vector<int> findBall(vector<vector<int>>& grid) {
        int n = grid.size(), m = grid[0].size();
        vector<int> orig(m);
        for (int i = 0; i < m; i++) orig[i] = i + 1;
        for (int i = 0; i < n; i++)
        {
            vector<int> tmp(m);
            for (int j = 0; j < m; j++)
            {
                if (grid[i][j] == 1)
                {
                    if (j + 1 < m && grid[i][j + 1] == 1)
                    {
                        tmp[j + 1] = orig[j];
                    }
                }
                else if (grid[i][j] == -1)
                {
                    if (j - 1 >= 0 && grid[i][j - 1] == -1)
                    {
                        tmp[j - 1] = orig[j];
                    }
                }
            }
            orig = tmp;
        }
        vector<int> res(m, -1);
        int k = orig.size();
        for (int i = 0; i < k; i++)
        {
            if (orig[i] != 0)
                res[orig[i] - 1] = i;
        }
        return res;
    }
};